/* 
 * 林子彦－qq:278805354.
 * 创建于2016-05-08 版权所有@请务拷贝
 */
$(document)
        //开启提交
        .ajaxStart(function () {
            //ui.loading_dialog();
        })
        .ajaxStop(function () {
            //ui.loading_delete();

        });
//表单提交
//$(".form1").submit(function () {

 //   var self = $(this);
  //  $.post(self.attr("action"), self.serialize(), success, "json");
  //  return false;



//});
$(".form1").bootstrapValidator(validator) .on('success.form.bv', function(e) {
   var self = $(this);
   $.post(self.attr("action"), self.serialize(), success, "json");
   return false;
});
function success(data) {
   // ui.loading_delete();
    if (data.code) {
        ui.success(data.msg);
        if (data.url == '') {
            setTimeout(function () {
                window.location.reload();
            }, 2000);
            return false;
        } else {
            setTimeout(function () {
                window.location.href = data.url;
            }, 2000);
        }
    } else {
        
        $(".form1").find(":submit").removeAttr("disabled");
        ui.error(data.msg);
       // $("input[type='submit']",".form1").removeAttr("disabled");
        return false;
    }

}
/**
 * 公共post提交
 * @param {type} url
 * @param {type} postdata
 * @returns {undefined}
 */
function common_url_post(url, postdata) {
    $.ajax({
        type: 'POST',
        url: url,
        data: postdata,
        async: false,
        dataType: 'json',
        success: function (data) {
            ui.loading_delete();
            if (data.code) {
                ui.success(data.msg);
                if (data.url == '') {
                    setTimeout(function () {
                        window.location.reload();
                    }, 2000);
                } else {
                    setTimeout(function () {
                        window.location.href = data.url;
                    }, 2000);
                }
            } else {
                ui.error(data.msg);
            }
        },
        error: function () {
            ui.error("系统异常");
        }
    });
}
/**
 * 公共删除
 */
$(".get_common_del").click(function(){
   var url=$(this).attr("href");
   var obj=$(this);
  
   
   $.ajax({
        type: 'GET',
        url: url,
        data: {},
        async: false,
        dataType: 'json',
        success: function (data) {
            ui.loading_delete();
            if (data.code) {
                ui.success(data.msg);
                obj.closest("tr").fadeOut(1000);
                return false;
            } else {
                ui.error(data.msg);
            }
        },
        error: function () {
            ui.error("系统异常");
        }
    });
   return false;
});

/**
 * 
 * @param {type} url
 * @param {type} postdata
 * @returns {undefined}
 */
$(".get_common_method").click(function(){
   var url=$(this).attr("href");
   var obj=$(this); 
   $.ajax({
        type: 'GET',
        url: url,
        data: {},
        async: false,
        dataType: 'json',
        success: function (data) {
            ui.loading_delete();
            if (data.code) {
                ui.success(data.msg);
               if (data.url == '') {
                    setTimeout(function () {
                        window.location.reload();
                    }, 2000);
                } else {
                    setTimeout(function () {
                        window.location.href = data.url;
                    }, 2000);
                }
                return false;
            } else {
                ui.error(data.msg);
            }
        },
        error: function () {
            ui.error("系统异常");
        }
    });
   return false;
});
/**
 * 公共get提交
 * @param {type} url
 * @param {type} postdata
 * @returns {undefined}
 */
function common_url_get(url, postdata) {
$.ajax({
        type: 'GET',
        url: url,
        data: postdata,
        async: false,
        dataType: 'json',
        success: function (data) {
            ui.loading_delete();
            if (data.code) {
                ui.success(data.msg);
                if (data.url == '') {
                    setTimeout(function () {
                        window.location.reload();
                    }, 2000);
                } else {
                    setTimeout(function () {
                        window.location.href = data.url;
                    }, 2000);
                }
            } else {
                ui.error(data.msg);
            }
        },
        error: function () {
            ui.error("系统异常");
        }
    });

}
/**
 * 生成url
 * @type type
 */
/**
 * 妯℃嫙TS鐨刄鍑芥暟锛岄渶瑕侀鍏堝畾涔塉S鍏ㄥ眬鍙橀噺SITE_URL鍜孉PPNAME
 * @param string url 閾炬帴鍦板潃
 * @param array params 閾炬帴鍙傛暟
 * @return string 缁勮鍚庣殑閾炬帴鍦板潃
 */
var U = function(url, params) {
	var website = SITE_URL+url;
	if(params) {
		//params = params.join('/');
		website = website + '/' + params+".html";
	}
	return website;
};


// +----------------------------------------------------------------------
// | Copyright (c) 2016－02－2 http://www.czsen.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 林群伟   <http://www.czsen.com>  版权所有
// +----------------------------------------------------------------------
//加载微信

var ui = {
    //错误信息
    error: function (message) {
        var html = '<div id="toast" ><div class="weui_mask_transparent"></div><div class="weui_toast_error"><i class="weui_icon_cancel"></i><span>' + message + '</span></div></div>';
        $(html).appendTo(document.body);
        setTimeout(function () {
           $("#toast").remove();
        }, 2000);
    },
    //成功信息
    success: function (message) {
        var html = '<div id="toast" ><div class="weui_mask_transparent"></div><div class="weui_toast_success"><i class="weui_icon_toastsuccess"></i><span>' + message + '</span></div></div>';
        $(html).appendTo(document.body);
        setTimeout(function () {
            $("#toast").remove();
        }, 2000);
    },
    //加载对话框
    loading_dialog: function () {
        var html = '<div id="loadingToast" class="weui_loading_toast"><div class="weui_mask_transparent"></div><div class="weui_toast"><div class="weui_loading"><div class="weui_loading_leaf weui_loading_leaf_0"></div><div class="weui_loading_leaf weui_loading_leaf_1"></div><div class="weui_loading_leaf weui_loading_leaf_2"></div><div class="weui_loading_leaf weui_loading_leaf_3"></div><div class="weui_loading_leaf weui_loading_leaf_4"></div><div class="weui_loading_leaf weui_loading_leaf_5"></div><div class="weui_loading_leaf weui_loading_leaf_6"></div><div class="weui_loading_leaf weui_loading_leaf_7"></div><div class="weui_loading_leaf weui_loading_leaf_8"></div><div class="weui_loading_leaf weui_loading_leaf_9"></div><div class="weui_loading_leaf weui_loading_leaf_10"></div><div class="weui_loading_leaf weui_loading_leaf_11"></div></div><p class="weui_toast_content">数据加载中</p></div></div>';
        $(html).appendTo(document.body);
    },
    //删除加载框
    loading_delete: function () {
        $("#loadingToast").remove();
    },
    //确认框
    comfirm: function (title, content, _callback) {
        var callback = "undefined" == typeof (_callback) ? $(o).attr('callback') : _callback;
        var html = '<div class="weui_dialog_confirm" id="dialog1" style="display: block;"><div class="weui_mask"></div><div class="weui_dialog"><div class="weui_dialog_hd"><strong class="weui_dialog_title">' + title + '</strong></div><div class="weui_dialog_bd">' + content + '</div><div class="weui_dialog_ft"><a href="javascript:;" class="weui_btn_dialog default">关闭</a><a href="javascript:;" class="weui_btn_dialog primary">确定</a></div></div></div>';
        $(html).appendTo(document.body);
        $("#dialog1").find('.weui_btn_dialog').one('click', function () {
            if ($(this).hasClass("primary")) {
                if ("function" == typeof (callback)) {
                    callback();
                } else {
                    eval(callback);
                }
            }
            $("#dialog1").remove();
        });

    },
    //确认框
    comfirm_refuse: function (title, content, _callback,refuse_callback) {
        var callback = "undefined" == typeof (_callback) ? $(o).attr('callback') : _callback;
        var refuse_callback = "undefined" == typeof (refuse_callback) ? $(o).attr('refuse_callback') : refuse_callback;
        var html = '<div class="weui_dialog_confirm" id="dialog1" style="display: block;"><div class="weui_mask"></div><div class="weui_dialog"><div class="weui_dialog_hd"><strong class="weui_dialog_title">' + title + '</strong></div><div class="weui_dialog_bd">' + content + '</div><div class="weui_dialog_ft"><a href="javascript:;" class="weui_btn_dialog ">关闭</a><a href="javascript:;" class="weui_btn_dialog default">拒绝</a><a href="javascript:;" class="weui_btn_dialog primary">确定</a></div></div></div>';
        $(html).appendTo(document.body);
        $("#dialog1").find('.weui_btn_dialog').one('click', function () {
            if ($(this).hasClass("primary")) {
                if ("function" == typeof (callback)) {
                    callback();
                } else {
                    eval(callback);
                }
            }
            if ($(this).hasClass("default")) {
                if ("function" == typeof (refuse_callback)) {
                    refuse_callback();
                } else {
                    eval(refuse_callback);
                }
            }
            
            $("#dialog1").remove();
        });

    },
    //弹出窗
    box: {
        load: function (requestUrl, title, callback, requestData, type) {
            if ("undefined" != typeof (type)) {
                var ajaxType = type;
            } else {
                var ajaxType = "GET";
            }
            jQuery.ajax({url: requestUrl,
                type: ajaxType,
                data: requestData,
                cache: false,
                dataType: 'html',
                success: function (html) {
                    if (!html.match("^\{(.+:.+,*){1,}\}$"))
                    {

                        var shtml = '<div id="gridSystemModal" class="modal fade" role="dialog" ><div class="modal-dialog" role="document"><div class="modal-content"><div class="modal-header"><button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button><h4 class="modal-title" id="gridSystemModalLabel">' + title + '</h4></div> ' + html + '</div></div></div>';
                        $(shtml).appendTo(document.body);
                        var vobj = $("#gridSystemModal");

                        vobj.modal({// wire up the actual modal functionality and show the dialog
                            "keyboard": true,
                            "show": true // ensure the modal is shown immediately
                        });
                        $(".close").bind('click', function () {
                            $(".modal-backdrop").remove();
                            $("body").removeClass("modal-open");
                            vobj.remove();

                        });



                        vobj.on("hidden.bs.modal", function (event) { // remove the event listeners when the dialog is dismissed
                            vobj.remove();
                            $(".modal-backdrop").remove();
                            $("body").removeClass("modal-open");
                        });
                        $(".btn-info").one('click', function () {
                            vobj.remove();
                            $(".modal-backdrop").remove();
                            $("body").removeClass("modal-open");
                            if ("undefined" == typeof (callback)) {
                                return true;
                            } else {
                                if ("function" == typeof (callback)) {
                                    callback();
                                } else {
                                    eval(callback);
                                }
                            }
                        });

                        $(".btn-default").one('click', function () {
                            $(".modal-backdrop").remove();
                            $("body").removeClass("modal-open");
                            vobj.remove();



                        });


                        $(".form2")
                                //开启提交
                                .ajaxStart(function () {
                                    ui.loading_dialog();
                                    $("button:submit").addClass("log-in").attr("disabled", true);
                                })
                                .ajaxStop(function () {
                                    ui.loading_delete();
                                    $("button:submit").removeClass("log-in").attr("disabled", false);
                                });
//表单提交
                        $(".form2").submit(function () {
                            var self = $(this);
                            $.post(self.attr("action"), self.serialize(), success, "json");
                            return false;

                            function success(data) {
                                var murl = data.url;
                                if (murl != '') {
                                    RE_URL = murl;
                                }
                                ui.loading_delete();
                                if (data.status) {
                                    var vobj = $("#gridSystemModal");
                                    vobj.remove();
                                    $(".modal-backdrop").remove();
                                    $("body").removeClass("modal-open");
                                    if ("undefined" == typeof (callback)) {
                                        return true;
                                    } else {
                                        if ("function" == typeof (callback)) {
                                            callback();
                                        } else {
                                            eval(callback);
                                        }
                                    }
                                    ui.success(data.info);


                                } else {

                                    ui.error(data.info);
                                }

                            }


                        });
                    }
                    else
                    {
                        //通过这种方法可将字符串转换为对象
                        var rdata = jQuery.parseJSON(html);
                        ui.error(rdata.info);
                        return false;
                    }

                }
            });
        }
    },
    //获取参数
    getEventArgs: function (node) {
        node.args || (node.args = ui.URI2Obj(node.getAttribute("event-args")));
        return node.args;
    },
    URI2Obj: function (uri) {
        if (!uri)
            return {};
        var obj = {},
                args = uri.split("&"),
                l, arg;
        l = args.length;
        while (l-- > 0) {
            arg = args[l];
            if (!arg) {
                continue;
            }
            arg = arg.split("=");
            obj[arg[0]] = arg[1];
        }
        return obj;
    }

}
/* 
 * 字符串操作类，主要用于字符串的添加和删除。例如：在字符串 "a,b,c"中添加字符串"d",或者直接删除某个字符串。 
 * 主要提供将给定字符串添加到某个字符串，中间用逗号分隔 
 */  
var StringOperate ={  
        separator    : ",",//字符串分隔符  
        baforeInsert : false,//字符串追加方式，默认false是在后面添加，true在追加到前面  
        isRepeat     : false,//追加的字符串是否可重复添加  
        isDeleteAll  : true,//删除所有匹配的字符串  
          
        //左边添加分隔符  
        lInsertSeparator : function(operateString){  
            if(operateString.indexOf(this.separator)  == 0)  
                return operateString;  
            return this.separator + operateString;  
        },  
        //右边添加分隔符  
        rInsertSeparator : function(operateString){  
            if(operateString.lastIndexOf(this.separator)  == (operateString.length - this.separator.length))  
                return operateString;  
            return operateString + this.separator;   
        },  
        //去除左边分隔符  
        lSeparatorTrim   : function(operateString){  
            if(operateString.indexOf(this.separator)  == 0)  
                return operateString.substring(1);  
            return operateString;  
        },  
        //去除右边的分隔符  
        rSeparatorTrim   : function(operateString){  
            if(operateString.lastIndexOf(this.separator)  == (operateString.length - this.separator.length))  
                return operateString.substring(0,operateString.length-1);  
            return operateString;  
        },  
        //追加字符串，将str字符串 追加到operateString中  
        add : function(operateString, str){  
            if( str  && str != ""){  
                if(this.isRepeat){//重复追加  
                    if(this.baforeInsert){//追加在开头  
                         return this.rSeparatorTrim(this.lSeparatorTrim(str + this.separator + operateString));  
                    }  
                    return this.rSeparatorTrim(this.lSeparatorTrim(operateString + this.separator + str));  
                }else{  
                    //开头和结尾都添加分隔符  
                    operateString = this.lInsertSeparator(this.rInsertSeparator(operateString));  
                    if(operateString.indexOf(this.separator + str + this.separator) == -1){  
                        if(this.baforeInsert){  
                            return this.rSeparatorTrim(this.lSeparatorTrim(str + operateString));  
                        }else{  
                            return this.rSeparatorTrim(this.lSeparatorTrim(operateString + str));  
                        }  
                    }  
                    return this.rSeparatorTrim(this.lSeparatorTrim(operateString));  
                }  
            }  
        },  
        //删除指定字符串  
        remove : function(operateString, str){  
            if(operateString && str && operateString != "" && str != ""){  
                //开头和结尾都添加分隔符  
                operateString = this.lInsertSeparator(this.rInsertSeparator(operateString));  
                if(this.isDeleteAll){  
                    operateString = operateString.replace(new RegExp(this.separator,"g"),this.separator + this.separator);  
                    //删除所有匹配的字符串  
                    operateString =  operateString.replace(new RegExp(this.separator + str +this.separator,"g"),this.separator);  
                    operateString = operateString.replace(new RegExp(this.separator+"{2,}","g"),this.separator);  
                }else{  
                    operateString =  operateString.replace(new RegExp(this.separator + str + this.separator),this.separator);  
                }  
                return this.rSeparatorTrim(this.lSeparatorTrim(operateString));  
            }  
        }  
          
          
};
var _core = function () {
    // 核心通用的加载源函数
    var obj = this;
    // 加载文件方法
    this._coreLoadFile = function () {

        var temp = new Array();
        var tempMethod = function (url, callback) {

            // 第二次调用的时候就不=0了
            var flag = 0;
            for (i in temp) {
                if (temp[i] == url) {
                    flag = 1;
                }
            }

            if (flag == 0) {
                // 未载入过
                temp[temp.length] = url;
                // JQuery的ajax载入文件方式，如果有样式文件，同理在此引入相关样式文件
                $.getScript(url, function () {

                    if ("undefined" != typeof (callback)) {
                        if ("function" == typeof (callback)) {

                            callback();
                        } else {

                            eval(callback);
                        }
                    }
                });
            } else {
                if ("undefined" != typeof (callback)) {
                    // 利用setTimeout 避免未定义错误
                    setTimeout(callback, 200);
                }
            }
        };
        // 返回内部包函数，供外部调用并可以更改temp的值
        return tempMethod;
    };


}

// 核心对象
var core = new _core();
core.loadFile = core._coreLoadFile();
